<?php

namespace app\api\controller;

use app\admin\model\Third;
use app\common\controller\Api;
use app\common\model\User;
use think\Db;

/**
 * 首页接口
 */
class Merchant extends Api
{
    protected $noNeedLogin = ['courseCategory','courseLabel'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
    public function info(){
        $user = $this->auth->getUser();
        $data = Db::name("merchant")->where(['user_id'=>$user->id])->find();
        $data['image'] = cdnurl($data['image'],true);
        $this->success("商家信息",$data);
    }
    public function infoEdit(){
        $user = $this->auth->getUser();
        $in_data['image'] = $this->request->post("image");
        $in_data['name'] = $this->request->post("name");
        $in_data['description'] = $this->request->post("description");
        $in_data['time'] = $this->request->post("time");
        $in_data['phone'] = $this->request->post("phone");
        $in_data['address'] = $this->request->post("address");
        Db::name("merchant")->where(['user_id'=>$user->id])->update($in_data);
        $this->success("信息编辑成功");
    }
    public function merchantTeacherAdd(){
        $user = $this->auth->getUser();
        $in_data['image'] = $this->request->post("image");
        $in_data['name'] = $this->request->post("name");
        $in_data['description'] = $this->request->post("description");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $in_data['merchant_user_id'] = $merchant_id;
        $in_data['phone'] = $this->request->post("phone");
        $data = Db::name("merchant_teacher")->where(['phone'=>$in_data['phone']])->find();
        if($data){
            $this->error("手机号已存在");
        }
       $user_data = Db::name("user")->where(['mobile'=> $in_data['phone']])->find();
        if(!$user_data){
            $this->error("手机号用户不存在");
        }
        $in_data['user_id'] = $user_data['id'];
        Db::name("user")->where(['id'=>$user_data['id']])->update(['is_teacher'=>1]);
        Db::name("merchant_teacher")->insertGetId($in_data);
        $this->success("老师添加成功");
    }
    public function merchantTeacherList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("merchant_teacher")->where(['merchant_user_id'=>$merchant_id])->order("id desc")->paginate()->toArray();
        // $list = $list->toArray();
        // var_dump($list);exit;
        //  var_dump($list['data']);
        foreach ($list['data'] as $k=>$v){
            // var_dump($v);exit;
            $list['data'][$k]['image'] =  cdnurl($v['image'], true);
        }
        // 将数据转化为数组
        //$list = $list->toArray();
        // 返回数据
        $this->success('老师列表', $list);
    }
    public function merchantTeacherListAll(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("merchant_teacher")->where(['merchant_user_id'=>$merchant_id])->select();
        // $list = $list->toArray();
        // var_dump($list);exit;
        //  var_dump($list['data']);
        foreach ($list as $k=>$v){
            // var_dump($v);exit;
            $list[$k]['image'] =  cdnurl($v['image'], true);
        }
        // 将数据转化为数组
        //$list = $list->toArray();
        // 返回数据
        $this->success('老师列表', $list);
    }
    public function merchantTeacherDelete(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("merchant_teacher")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        Db::name("merchant_teacher")->where(['id'=>$id])->delete();
        $this->success("老师删除成功");
    }
    public function merchantTeacherEdit(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("merchant_teacher")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        $data = Db::name("merchant_teacher")->where(['id'=>$id])->find();
        $data['image']=cdnurl($data['image'],true);
        $this->success("老师详情",$data);
    }

    /**
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function merchantTeacherEditPost(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("merchant_teacher")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        $in_data['image'] = $this->request->post("image");
        $in_data['name'] = $this->request->post("name");
        $in_data['description'] = $this->request->post("description");
        $in_data['merchant_user_id'] = $merchant_id;
        $in_data['phone'] = $this->request->post("phone");
        $data1 = Db::name("merchant_teacher")->where(['phone'=>$in_data['phone']])->where('id','<>',$id)->find();
        if($data1){
            $this->error("手机号已存在");
        }
        $user_data = Db::name("user")->where(['mobile'=> $in_data['phone']])->find();
        if(!$user_data){
            $this->error("手机号用户不存在");
        }
        $in_data['user_id'] = $user_data['id'];
        Db::name("user")->where(['id'=>$user_data['id']])->update(['is_teacher'=>1]);
        Db::name("merchant_teacher")->where(['id'=>$id])->update($in_data);
        $this->success("老师编辑成功");
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function courseCategory(){
        $user = $this->auth->getUser();
        $merchant = Db::name("merchant")->where(['user_id'=>$user->id])->find();
        $course_label_id = $merchant["label_id"];

        $list = Db::name("course_category")->where(['course_label_id'=>$course_label_id])->select();
        $this->success("课程分类",$list);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function courseLabel(){
        $list = Db::name("course_label")->select();
        $this->success("课程标签",$list);
    }

    /**
     * @return void
     * 课程添加
     */
    public function courseAdd(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');

        $merchant = Db::name("merchant")->where(['user_id'=>$user->id])->find();
        $course_label_id = $merchant["label_id"];
        $type = $this->request->post("type");
        $user_id = $this->request->post("user_id");
        $str_time = $this->request->post("str_time");
        $end_time = $this->request->post("end_time");

        $in_data['images'] = $this->request->post("images");
        $images = $this->request->post("images");
        $images_arr = explode(',',$images);
        $in_data['image'] = $images_arr[0];
        $in_data['type'] = $type??0;
        $in_data['str_time'] = $str_time?strtotime($str_time):0;
        $in_data['end_time'] = $end_time?strtotime($end_time):0;
        $in_data['name'] = $this->request->post("name");
        $in_data['description'] = $this->request->post("description");
        $in_data['merchant_user_id'] = $merchant_id;
        $in_data['price'] = $this->request->post("price");
        $in_data['merchant_teacher_id'] = $this->request->post("merchant_teacher_id");
        $in_data['course_category_id'] = $this->request->post("course_category_id");
        $in_data['course_label_id'] = $course_label_id;
        $in_data['sum'] = $this->request->post("sum");
        $in_data['createtime'] = time();
        //var_dump($in_data);
        $course_id = Db::name("course")->insertGetId($in_data);
       /* if($type == 1){
            $out_trade_no = date("YmdHis") . mt_rand(100000, 999999);
            $in_data_order['out_trade_no'] = $out_trade_no;
            $in_data_order['course_id'] = $course_id;
            $in_data_order['pay_type'] = 2;
            $in_data_order['merchant_user_id'] = $merchant_id;
            $in_data_order['user_id'] = $user_id;
            $in_data_order['price'] = $this->request->post("price");
            $in_data_order['sum'] = 1;
            $in_data_order['createtime'] = time();
            $in_data_order['status'] = 0;
            $in_data_order['pay_money'] = $this->request->post("price");
            $in_data_order['label_name'] = Db::name("course_label")->where(['id'=>$course_label_id])->value("name");

            $in_status = Db::name("course_order")->insertGetId($in_data_order);


            $in_data_card['user_id'] = $user_id;
            $in_data_card['order_id'] = $in_status;
            $in_data_card['course_id'] = $course_id;
            $in_data_card['end_time'] = $end_time?$end_time:null;

            $in_data_card['sum'] = 1;
            $in_data_card['surplus_sum'] = 0;
            $in_data_card['merchant_user_id'] = $merchant_id;
            $in_data_card['merchant_teacher_id'] = $this->request->post("merchant_teacher_id");
            $in_data_card['createtime'] = time();
            $in_data_card['type'] = 1;
            $user_recharge_card_id = Db::name("user_recharge_card")->insertGetId($in_data_card);
           // $prein_data['yy_time'] = $timetable_data['c_time'];
            $prein_data['type'] = 1;
            $prein_data['course_id'] = $course_id;
            $prein_data['course_order_id'] = $in_status;
            $prein_data['user_recharge_card_id'] = $user_recharge_card_id;
            //$prein_data['course_time_ids'] = $course_time_ids;
            $prein_data['merchant_teacher_id'] = $this->request->post("merchant_teacher_id");
            $prein_data['user_id'] = $user_id;
           // $prein_data['day'] = strtotime($pre_day);
            $prein_data['createtime'] = time();

            Db::name("course_order_pre")->insertGetId($prein_data);
        }*/
        $this->success("课程添加成功");
    }
    public function courseList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("course")->where(['merchant_user_id'=>$merchant_id])->order("id desc")->paginate()->toArray();
        // $list = $list->toArray();
        // var_dump($list);exit;
        //  var_dump($list['data']);
        foreach ($list['data'] as $k=>$v){
            // var_dump($v);exit;
            $list['data'][$k]['image'] =  cdnurl($v['image'], true);
        }
        // 将数据转化为数组
        //$list = $list->toArray();
        // 返回数据
        $this->success('课程列表', $list);
    }
    public function allcourseList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("course")->where(['merchant_user_id'=>$merchant_id])->select();
        $this->success('课程列表', $list);
    }
    public function courseDelete(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("course")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        Db::name("course")->where(['id'=>$id])->delete();
        $this->success("课程删除成功");
    }
    public function courseEdit(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("course")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        $data = Db::name("course")->where(['id'=>$id])->find();
        $data['image']=cdnurl($data['image'],true);
        $data['end_time']=date('Y-m-d H:i:s',$data['end_time']);
        $data['str_time']=date('Y-m-d H:i:s',$data['str_time']);
        $this->success("课程详情",$data);
    }

    /**
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function courseEditPost(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("course")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        $str_time = $this->request->post("str_time");
        $end_time = $this->request->post("end_time");
        $type = $this->request->post("type",0);
        $merchant = Db::name("merchant")->where(['user_id'=>$user->id])->find();
        $course_label_id = $merchant["label_id"];

        $in_data['images'] = $this->request->post("images");
        $images = $this->request->post("images");
        $images_arr = explode(',',$images);
        $in_data['image'] = $images_arr[0];
        $in_data['type'] = $type;
        $in_data['str_time'] = $str_time?strtotime($str_time):0;
        $in_data['end_time'] = $end_time?strtotime($end_time):0;
        $in_data['name'] = $this->request->post("name");
        $in_data['description'] = $this->request->post("description");
        $in_data['price'] = $this->request->post("price");
        $in_data['merchant_teacher_id'] = $this->request->post("merchant_teacher_id");
        $in_data['course_category_id'] = $this->request->post("course_category_id");
        $in_data['course_label_id'] = $course_label_id;
        $in_data['sum'] = $this->request->post("sum");
        $in_data['updatetime'] = time();
        Db::name("course")->where(['id'=>$id])->update($in_data);
        $this->success("课程编辑成功");
    }
    /**
     * @return void
     * 课程添加
     */
    public function goodsAdd(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');

        $merchant = Db::name("merchant")->where(['user_id'=>$user->id])->find();
        $label_id = $merchant["label_id"];

        $in_data['image'] = $this->request->post("image");
        $in_data['name'] = $this->request->post("name");
        $in_data['description'] = $this->request->post("description");
        $in_data['merchant_user_id'] = $merchant_id;
        $in_data['price'] = $this->request->post("price");
        $in_data['content'] = $this->request->post("content");
       // $in_data['goods_category_id'] = $this->request->post("goods_category_id");
        $in_data['label_id'] = $label_id;
        $in_data['createtime'] = time();
        Db::name("goods")->insertGetId($in_data);
        $this->success("商品添加成功");
    }
    public function goodsList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("goods")->where(['merchant_user_id'=>$merchant_id])->order("id desc")->paginate()->toArray();
        // $list = $list->toArray();
        // var_dump($list);exit;
        //  var_dump($list['data']);
        foreach ($list['data'] as $k=>$v){
            // var_dump($v);exit;
            $list['data'][$k]['image'] =  cdnurl($v['image'], true);
        }
        // 将数据转化为数组
        //$list = $list->toArray();
        // 返回数据
        $this->success('商品列表', $list);
    }
    public function goodsDelete(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("goods")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        Db::name("goods")->where(['id'=>$id])->delete();
        $this->success("商品删除成功");
    }
    public function goodsEdit(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("goods")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        $data = Db::name("goods")->where(['id'=>$id])->find();
        $data['image']=cdnurl($data['image'],true);
        $this->success("商品详情",$data);
    }

    /**
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function goodsEditPost(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("goods")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        $merchant = Db::name("merchant")->where(['user_id'=>$user->id])->find();
        $label_id = $merchant["label_id"];

        $in_data['image'] = $this->request->post("image");
        $in_data['name'] = $this->request->post("name");
        $in_data['description'] = $this->request->post("description");
        $in_data['price'] = $this->request->post("price");
        $in_data['content'] = $this->request->post("content");
       // $in_data['goods_category_id'] = $this->request->post("goods_category_id");
        $in_data['label_id'] = $label_id;
        $in_data['updatetime'] = time();
        Db::name("goods")->where(['id'=>$id])->update($in_data);
        $this->success("商品编辑成功");
    }
    public function couponAdd(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $in_data['name'] = $this->request->post("name");
        $in_data['money'] = $this->request->post("money");
        $in_data['number'] = $this->request->post("number");
        $in_data['give_num'] = $this->request->post("give_num");
        $in_data['allow_num'] = $this->request->post("allow_num");
        $in_data['begintime'] = strtotime($this->request->post("begintime"));
        $in_data['endtime'] = strtotime($this->request->post("endtime"));
        $in_data['use_begtime'] = strtotime($this->request->post("use_begtime"));
        $in_data['use_endtime'] = strtotime($this->request->post("use_endtime"));
        $in_data['content'] = $this->request->post("content");
        $in_data['score'] = $this->request->post("score");
        $in_data['merchant_user_id'] = $merchant_id;
        $in_data['createtime'] = time();
        $all_score = $in_data['give_num'] * $in_data['score'];
        if($all_score > $user->score){
            $this->error("积分不足");
        }
        User::score(-$all_score, $user->id, '添加优惠券退回积分',0,$merchant_id);
        $id = Db::name("coupon")->insertGetId($in_data);
        if($id){
            $this->success("优惠券发放成功");
        }else{
            $this->error("优惠券发放失败");
        }
    }
    public function couponList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $status= $this->request->post("status",0);//0未开始 1 进行中 2 已结束
        $where['id'] = ['>',0];
        if($status == 0){
            $where['begintime'] = ['>',time()];
        }elseif ($status == 1){
            $where['begintime'] = ['<', time()];
            $where['endtime'] = ['>', time()];
        }elseif ($status == 2){
            $where['endtime'] = ['<', time()];
        }
        $list = Db::name("coupon")->where(['merchant_user_id'=>$merchant_id])->where($where)->order("id desc")->paginate()->toArray();
        foreach ($list['data'] as $k=>$v){
            $list['data'][$k]['endtime'] = date('Y-m-d H:i:s',$v['endtime']);
        }
        $this->success('优惠券列表', $list);
    }
    public function couponDelete(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $id = $this->request->post("id");
        $data = Db::name("coupon")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        Db::name("coupon")->where(['id'=>$id])->delete();
        $this->success("优惠券删除成功");
    }
    public function couponEdit(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $id = $this->request->post("id");
        $data = Db::name("coupon")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        $data = Db::name("coupon")->where(['id'=>$id])->find();
        $this->success("优惠券详情",$data);
    }

    /**
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function couponEditPost(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $id = $this->request->post("id");
        $data = Db::name("coupon")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        $all_score_old = ($data['give_num']-$data['received_num']) * $data['score'];

        $in_data['name'] = $this->request->post("name");
        $in_data['money'] = $this->request->post("money");
        $in_data['number'] = $this->request->post("number");
        $in_data['give_num'] = $this->request->post("give_num");
        $in_data['allow_num'] = $this->request->post("allow_num");
        $in_data['begintime'] = strtotime($this->request->post("begintime"));
        $in_data['endtime'] = strtotime($this->request->post("endtime"));
        $in_data['use_begtime'] = strtotime($this->request->post("use_begtime"));
        $in_data['use_endtime'] = strtotime($this->request->post("use_endtime"));
        $in_data['content'] = $this->request->post("content");
        $in_data['score'] = $this->request->post("score");
        $in_data['updatetime'] = time();
        $all_score_new = $in_data['give_num'] * $in_data['score'];
        $end_score = $all_score_old > $all_score_new;
        if($end_score >0 ){

            User::score($end_score, $user->id, '编辑优惠券退回积分',0,$merchant_id);
        }else{
            if(abs($end_score) > $user->score){
                $this->error("积分不足");
            }
            User::score($end_score, $user->id, '编辑优惠券扣除积分',0,$merchant_id);
        }
        Db::name("coupon")->where(['id'=>$id])->update($in_data);
        $this->success("优惠券编辑成功");
    }
    public function rechargeAdd(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $type = $this->request->post("type");
        $phone = $this->request->post("phone");
        $score = $this->request->post("score");
        $image = $this->request->post("image");
        $fixed_score = $this->request->post("fixed_score");
        $course_id = $this->request->post("course_id");
        $card_id = $this->request->post("card_id");
        $card_day = $this->request->post("card_day");
        if($type == 1){
            if(!$phone || !$score) {
                $this->error("缺少必传参数");
            }
        }elseif ($type == 2){
            if(!$phone || !$course_id || !$card_day) {
                $this->error("缺少必传参数");
            }
        }else{
            $this->error("参数有误");
        }
        $in_data['str_time'] = strtotime($this->request->post("str_time"));
        $in_data['end_time'] = strtotime($this->request->post("end_time"));
        $in_data['phone'] = $this->request->post("phone");
        $in_data['score'] = $this->request->post("score");
        $in_data['fixed_score'] = $this->request->post("fixed_score");
        $in_data['image'] = $this->request->post("image");
        $in_data['type'] = $this->request->post("type");
        $in_data['course_id'] = $this->request->post("course_id");
        $in_data['card_day'] = $card_day;
        $in_data['merchant_user_id'] = $merchant_id;
        $in_data['createtime'] = time();
        $user_data = Db::name("user")->where(['mobile'=>$in_data['phone']])->find();
        if(!$user_data){
            $this->success("充值号码不存在");
        }
        $in_data['user_id'] = $user_data['id'];
        $id = Db::name("recharge")->insertGetId($in_data);
        if($id){
            $this->success("充值添加成功");
        }else{
            $this->error("充值添加失败");
        }
    }
    public function rechargeScore(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $phone = $this->request->post("phone");
        $score = Db::name("recharge")->where(['merchant_user_id'=>$merchant_id,'phone'=>$phone,'status'=>1])->sum("score");
        $fixed_score = Db::name("recharge")->where(['phone'=>$phone,'status'=>1])->sum("fixed_score");
        $this->success('充值提交页面获取用户的积分', $score+$fixed_score);
    }
    public function rechargeList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("recharge")->where(['merchant_user_id'=>$merchant_id])->order("id desc")->paginate()->toArray();
        foreach ($list['data'] as $k=>$v){
            $list['data'][$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);
            $list['data'][$k]['sum_score'] = $v['score']+$v['fixed_score'];
        }
        $this->success('优惠券列表', $list);
    }
    public function cardList(){
        $list = Db::name("card")->select();
        foreach ($list as $k=>$v){
            $list[$k]['course_list'] = Db::name("course")->where('id','in',$v['course_ids'])->select();
        }
        $this->success("课程卡列表",$list);
    }
    public function merchantScore(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $in_data['image'] = $this->request->post("image");
        $in_data['score'] = $this->request->post("score");
        if(!$in_data['image'] || !$in_data['score']){
            $this->error("缺少必传参数");
        }
//        $user_data = Db::name("user")->where(['mobile'=>$in_data['phone']])->find();
//        if(!$user_data){
//            $this->error("用户不存在");
//        }

        $in_data['merchant_user_id'] = $merchant_id;
        $in_data['createtime'] = time();
        $in_data['status'] = 0;//0未审核 1 审核通过 2 审核驳回
        $in_data['user_id'] = $user->id;


        //\app\common\model\User::score($in_data['score'], $user_data['id'], "商家预存积分",1,$user->id);
        Db::name("merchant_score")->insertGetId($in_data);
        $this->success("预存积分添加成功");
    }
    public function getMerchantScore(){
        $user = $this->auth->getUser();
        $this->success("获取积分",$user->score);
    }
    public function merchantScoreList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("merchant_score")->where(['merchant_user_id'=>$merchant_id])->order("id desc")->paginate()->toArray();
        foreach ($list['data'] as $k=>$v){
            // var_dump($v);exit;
            $list['data'][$k]['createtime'] =  date("Y-m-d H:i:s",$v['createtime']);
        }
        $this->success('预存积分列表', $list);
    }
    public function courseOrderList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->whereOr(['examine_user_ids_str'=>['like','%,'.$user->id.',%']])->column('id');
//echo Db::name("merchant")->getLastSql();
        $list = Db::name("course_order")->where(['merchant_user_id'=>['in',$merchant_id]])->order("id desc")->paginate()->toArray();
        //echo Db::name("course_order")->getLastSql();
        // $list = $list->toArray();
        // var_dump($list);exit;
        //  var_dump($list['data']);
        foreach ($list['data'] as $k=>$v){
            $course_data = Db::name("course")->where(['id'=>$v['course_id']])->find();
            // var_dump($v);exit;
            $list['data'][$k]['image'] =  cdnurl($course_data['image'], true);
            $list['data'][$k]['name'] =  $course_data['name'];
            $list['data'][$k]['nickname'] =  Db::name("user")->where(['id'=>$v['user_id']])->value("nickname");
            $list['data'][$k]['createtime'] =  date('Y-m-d H:i:s',$v['createtime']);
        }
        // 将数据转化为数组
        //$list = $list->toArray();
        // 返回数据
        $this->success('课程列表', $list);
    }
    public function courseOrderRefund(){
        $order_id = $this->request->post("order_id");
        $score = $this->request->post("score");
        $data = Db::name("course_order")->where(['id'=>$order_id])->find();
        if(!$data){
            $this->error("数据错误");
        }
        if($data['status'] != 3){
            $this->error("不符合退款条件");
        }
        Db::name("course_order")->where(['id'=>$order_id])->update(['status'=>4]);
        //删除课程包
        Db::name("user_recharge_card")->where(['order_id'=>$order_id])->delete();
        \app\common\model\User::score($score, $data['user_id'], '订单退款',0,$data['merchant_user_id'],$data['id']);
        $this->success("退款成功");
    }
    public function scoreFixedLog(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("user_score_log")->where(['merchant_user_id'=>$merchant_id,'is_fixed'=>1,'score'=>['<',0]])->group('user_id')->field("id,user_id")->order("id desc")->paginate()->toArray();
        foreach ($list['data'] as $k=>$v){
            $usr_data = Db::name("user")->where(['id'=>$v['user_id']])->find();
            $list['data'][$k]['avatar'] =  cdnurl($usr_data['avatar'], true);
            $list['data'][$k]['nickname'] =  $usr_data['nickname'];
            $list['data'][$k]['mobile'] =  $usr_data['mobile'];
            $list['data'][$k]['fixed_score'] =  $usr_data['fixed_score'];
            $list['data'][$k]['xh_count'] = Db::name("user_score_log")->where(['merchant_user_id'=>$merchant_id,'is_fixed'=>1,'user_id'=>$v['user_id'],'score'=>['<',0]])->count();
            $xh_sum = Db::name("user_score_log")->where(['merchant_user_id'=>$merchant_id,'is_fixed'=>1,'user_id'=>$v['user_id'],'score'=>['<',0]])->sum("score");
            $list['data'][$k]['xh_sum'] = abs($xh_sum);
        }
        $this->success("固定积分消费",$list);
    }
    public function scoreFixedLogEdit(){
        $user_id = $this->request->post("user_id");
        $user = $this->auth->getUser();
        $user_data = Db::name("user")->where(['id'=>$user_id])->find();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("user_score_log")->where(['merchant_user_id'=>$merchant_id,'user_id'=>$user_id,'is_fixed'=>1,'score'=>['<',0]])->field("id,user_id,order_id,score,createtime")->order("id desc")->paginate()->toArray();
        foreach ($list['data'] as $k=>$v){
            $order_data = Db::name("course_order")->where(['id'=>$v['order_id']])->find();
            $course_data = Db::name("course")->where(['id'=>$order_data['course_id']])->find();
            $list['data'][$k]['image'] =  cdnurl($course_data['image'], true);
            $list['data'][$k]['nbame'] =  $course_data['name'];
            $list['data'][$k]['createtime'] =  date('Y.m.d',$v['createtime']);
        }
        $xh_count = Db::name("user_score_log")->where(['merchant_user_id'=>$merchant_id,'is_fixed'=>1,'user_id'=>$user_id,'score'=>['<',0]])->count();
        $re_data['list'] = $list;
        $re_data['xh_count'] = $xh_count;
        $re_data['nickname'] = $user_data['nickname'];
        $re_data['avatar'] = cdnurl($user_data['avatar'],true);
        $re_data['sy_score'] = self::getMerchantScoreone($user_id,$merchant_id);
        $this->success("用户消费详情",$re_data);
    }
    public function scoreFixedLogchar(){
        $user_id = $this->request->post("user_id");
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("user_score_log")->where(['merchant_user_id'=>$merchant_id,'is_fixed'=>1,'user_id'=>$user_id,'score'=>['>',0]])->field("id,user_id,memo,score,createtime")->order("id desc")->paginate()->toArray();
        foreach ($list['data'] as $k=>$v){
            $list['data'][$k]['createtime'] =  date('Y.m.d H:i',$v['createtime']);
        }
        $this->success("固定积分的充值记录",$list);
    }
    public function scoreFixedLogOrder(){
        $order_id = $this->request->post("order_id");
        $order_data = Db::name("course_order")->field("out_trade_no,price,course_id,createtime,fixed_score,score")->where(['id'=>$order_id])->find();
        $course_data = Db::name("course")->where(['id'=>$order_data['course_id']])->find();
        $order_data['image'] =  cdnurl($course_data['image'], true);
        $order_data['nbame'] =  $course_data['name'];
        $order_data['description'] =  $course_data['description'];
        $order_data['createtime'] =  date('Y-m-d H:i:s',$order_data['createtime']);
        $this->success("订单详情",$order_data);
    }
    public static function getMerchantScoreone($user_id,$merchant_user_id){
        $list = Db::name("user_score_log")->where(['user_id'=>$user_id,'merchant_user_id'=>$merchant_user_id,'is_fixed'=>1])->select();
        $vlu = 0;
        if($list){
            foreach ($list as $k=>$v){
                if($v['before'] > $v['after']){
                    $vlu -= abs($v['score']);
                }else{
                    $vlu += abs($v['score']);
                }
            }
        }
        return $vlu;
    }
    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 积分核销
     */
    public function scoreoff(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $in_data['phone'] = $this->request->post("phone");
        $in_data['course_id'] = $this->request->post("course_id");
        $in_data['score'] = $this->request->post("score");
        $in_data['merchant_user_id'] = $merchant_id;
        $user_data = Db::name("user")->where(['mobile'=>$in_data['phone']])->find();

        if(!$user_data){
            $this->error("用户不存在");
        }
        $merchant_score = self::getMerchantScoreone($user_data['id'],$merchant_id);
        if($merchant_score < $in_data['score']){
            $this->error("积分不足");
        }
        $in_data['user_id'] = $user_data['id'];
        $in_data['createtime'] = time();
        Db::name("scoreoff")->insertGetId($in_data);
        $this->success("积分核销提交成功");
    }
    /**
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 积分明细
     */
    public function getScoreLog(){
        $is_fixed = $this->request->post("is_fixed", 0);//0自由积分 1固定积分
        $page = $this->request->post("page", 1);
        $per_page = $this->request->post("per_page", 15);
        $user = $this->auth->getUser();

        $where['user_id'] = $user->id;
        $where['is_fixed'] = $is_fixed;
        $total = Db::name("user_score_log")
            ->where($where)->count();
        $str_num = ($page - 1) * $per_page;
        $list = Db::name("user_score_log")
            ->where($where)
            ->order("id desc")
            ->limit($str_num, $per_page)
            ->select();
        foreach ($list as $k=>$v){
            $list[$k]['createtime'] = date('Y-m-d H:i:s',$v['createtime']);

        }
        $re_data['list'] = $list;
        if($is_fixed == 1){
            $re_data['score'] = $this->auth->fixed_score;
        }else{
            $re_data['score'] = $this->auth->score;
        }
        $re_data['avatar'] =  cdnurl($user->avatar, true);
        $re_data['nickname'] =  $user->nickname;
        $re_data['current_page'] = $page;
        $re_data['last_page'] = ceil($total/$per_page);
        $re_data['total'] = $total;
        $re_data['per_page'] = $per_page;
        $this->success('商家钱包', $re_data);
    }
    public function courseOrderPreList(){
        $course_id = $this->request->post("course_id");
        $list = Db::name("course_order_pre")->where(['course_id'=>$course_id])->field("id,createtime,status,user_id,day")->order("id desc")->paginate()->toArray();
        foreach ($list['data'] as $k=>$v){
            $usr_data = Db::name("user")->where(['id'=>$v['user_id']])->find();
            $list['data'][$k]['avatar'] =  cdnurl($usr_data['avatar'], true);
            $list['data'][$k]['nickname'] =  $usr_data['nickname'];
            $list['data'][$k]['day'] =  date('Y.m.d',$v['day']);
        }
        $this->success("课程预约记录",$list);
    }
    public function goodsOrderList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->whereOr(['examine_user_ids_str'=>['like','%,'.$user->id.',%']])->column('id');
        //echo Db::name("merchant")->getLastSql();
        $list = Db::name("goods_order")->where(['merchant_user_id'=>['in',$merchant_id],'status'=>['>',0]])->field("id,goods_id,user_id,createtime,status")->order("id desc")->paginate()->toArray();
        //echo Db::name("goods_order")->getLastSql();
        foreach ($list['data'] as $k=>$v){
            $doods_data = Db::name("goods")->where(['id'=>$v['goods_id']])->find();
            $list['data'][$k]['image'] =  cdnurl($doods_data['image'], true);
            $list['data'][$k]['goods_name'] =  $doods_data['name'];
            $list['data'][$k]['createtime'] =  date('Y.m.d',$v['createtime']);
            $usr_data = Db::name("user")->where(['id'=>$v['user_id']])->find();
            $list['data'][$k]['mobile'] =  $usr_data['mobile'];
            $list['data'][$k]['nickname'] =  $usr_data['nickname'];
        }
        $this->success("课程订单列表",$list);
    }
    public function goodsOrderOff(){
        $goods_id = $this->request->post("goods_id");
        $user = $this->auth->getUser();
        $goods_order_data = Db::name("goods_order")->where(['id'=>$goods_id])->find();
        //echo Db::name("course_order_pre")->getLastSql();
        if(!$goods_order_data){
            $this->error("数据有误");
        }
        if($goods_order_data['status'] != 1){
            $this->error("不符合核销条件");
        }

        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->whereOr(['examine_user_ids_str'=>['like',','.$user->id.',']])->column('id');
        if(!in_array($goods_order_data['merchant_user_id'],$merchant_id)){
            $this->error("您没有核销权限");
        }
        Db::name("goods_order")->where(['id'=>$goods_id])->update(['status'=>5]);
        $this->success("核销成功");
    }
    public function userRechargeList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("user_score_log")
            ->where(['merchant_user_id'=>$merchant_id,'surplus_score'=>['>',0]])
            ->field('id,merchant_user_id,surplus_score,end_time,user_id')
            ->order("id desc")->paginate()->toArray();
        //echo Db::name("user_score_log")->getLastSql();
        // $list = $list->toArray();
        // var_dump($list);exit;
        //  var_dump($list['data']);
        foreach ($list['data'] as $k=>$v){
            $usr_data = Db::name("user")->where(['id'=>$v['user_id']])->find();
            $list['data'][$k]['avatar'] =  cdnurl($usr_data['avatar'], true);
            $list['data'][$k]['nickname'] =  $usr_data['nickname'];
            $list['data'][$k]['mobile'] =  $usr_data['mobile'];
            $list['data'][$k]['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
        }
        // 将数据转化为数组
        //$list = $list->toArray();
        // 返回数据
        $re_data['fixed_score'] = $user->fixed_score;
        $re_data['list'] = $list;
        $this->success('用户充值列表', $re_data);
    }
    public function courseShow(){
        $course_id = $this->request->post("course_id");
        $is_show = $this->request->post("is_show");
        if(!$course_id || !$is_show){
            $this->error("缺少必传参数");
        }
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data =Db::name("course")->where(['id'=>$course_id,'merchant_user_id'=>$merchant_id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        Db::name("course")->where(['id'=>$course_id])->update(['is_show'=>$is_show]);
        $this->success("修改成功");
    }
    public function goodsShow(){
        $goods_id = $this->request->post("goods_id");
        $is_show = $this->request->post("is_show");
        if(!$goods_id || !$is_show){
            $this->error("缺少必传参数");
        }
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data =Db::name("goods")->where(['id'=>$goods_id,'merchant_user_id'=>$merchant_id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        Db::name("goods")->where(['id'=>$goods_id])->update(['is_show'=>$is_show]);
        $this->success("修改成功");
    }

    /**
     * @return void
     * 课程包添加
     */
    public function wrapAdd(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $image = $this->request->post("image");
        $name = $this->request->post("name");
        $description = $this->request->post("description");
        $price = $this->request->post("price");
        $in_add['merchant_user_id'] = $merchant_id;
        $in_add['image'] = $image;
        $in_add['name'] = $name;
        $in_add['description'] = $description;
        $in_add['price'] = $price;
        $in_add['createtime'] = time();
        $in_status = Db::name("course_wrap")->insertGetId($in_add);
        if($in_status){
            $this->success("添加成功");
        }else{
            $this->error("添加失败");
        }
    }
    public function wrapList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("course_wrap")->where(['merchant_user_id'=>$merchant_id])->order("id desc")->paginate()->toArray();
        // $list = $list->toArray();
        // var_dump($list);exit;
        //  var_dump($list['data']);
        foreach ($list['data'] as $k=>$v){
            // var_dump($v);exit;
            $list['data'][$k]['image'] =  cdnurl($v['image'], true);
        }
        // 将数据转化为数组
        //$list = $list->toArray();
        // 返回数据
        $this->success('课程包列表', $list);
    }
    public function wrapDelete(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("course_wrap")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        Db::name("course_wrap")->where(['id'=>$id])->delete();
        $this->success("课程包删除成功");
    }
    public function wrapEdit(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("course_wrap")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }
        $data = Db::name("course_wrap")->where(['id'=>$id])->find();
        $data['image']=cdnurl($data['image'],true);
        $this->success("课程包详情",$data);
    }

    /**
     * @return void
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function wrapEditPost(){
        $user = $this->auth->getUser();
        $id = $this->request->post("id");
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $data = Db::name("course_wrap")->where(['merchant_user_id'=>$merchant_id,'id'=>$id])->find();
        if(!$data){
            $this->error("数据有误");
        }

        $in_data['image'] = $this->request->post("image");
        $in_data['name'] = $this->request->post("name");
        $in_data['description'] = $this->request->post("description");
        $in_data['price'] = $this->request->post("price");
        $in_add['merchant_user_id'] = $merchant_id;
        // $in_data['goods_category_id'] = $this->request->post("goods_category_id");
        //$in_data['updatetime'] = time();
        Db::name("course_wrap")->where(['id'=>$id])->update($in_data);
        $this->success("课程包编辑成功");
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 课程包列表
     */
    public function wrapListAll(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("course_wrap")->where(['merchant_user_id'=>$merchant_id])->select();
        $this->success("课程包列表",$list);
    }
    public function wrapRechargeAdd(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $image = $this->request->post("image");
        $phone = $this->request->post("phone");
        $sum = $this->request->post("sum");
        $is_pay = $this->request->post("is_pay",0);
        $course_wrap_id = $this->request->post("wrap_id");
        $wrap_data = Db::name("course_wrap")->where(['id'=>$course_wrap_id])->find();
        if(!$wrap_data){
            $this->error("数据有误");
        }
        $user_data = Db::name("user")->where(['mobile'=>$phone])->find();
        if(!$user_data){
            $this->error("手机号错误");
        }
        $all_price = $wrap_data['price']*$sum;
        $in_add['merchant_user_id'] = $merchant_id;
        $in_add['course_wrap_id'] = $course_wrap_id;
        $in_add['sum'] = $sum;
        $in_add['image'] = $image;
        $in_add['phone'] = $phone;
        $in_add['price'] = $wrap_data['price'];
        $in_add['name'] = $wrap_data['name'];
        $in_add['all_price'] = $all_price;
        $user_zongjifen = $user_data['score'] + $user_data['fixed_score'];
        if($user_zongjifen < $all_price && $is_pay == 0){
            $this->error("用户积分不足");
        }
        $in_add['createtime'] = time();
        $in_status = Db::name("course_wraprecharge")->insertGetId($in_add);

        if($in_status){
            $in_data_card['user_id'] = $user_data['id'];
            $in_data_card['order_id'] = $in_status;
            $in_data_card['sum'] = $sum;
            $in_data_card['merchant_user_id'] = $merchant_id;
            $in_data_card['surplus_sum'] = $sum;
            $in_data_card['createtime'] = time();
            $in_data_card['type'] = 3;
            Db::name("user_recharge_card")->insertGetId($in_data_card);
            if($is_pay == 0){
                if($user_data['fixed_score'] > $all_price){
                    \app\common\model\User::score(-$all_price, $user_data['id'], "购买课程包消耗积分",1,$merchant_id);
                }else{
                    \app\common\model\User::score(-$user_data['fixed_score'], $user_data['id'], "购买课程包消耗积分",1,$merchant_id);
                    $sheng_price = $all_price - $user_data['fixed_score'];
                    \app\common\model\User::score(-$sheng_price, $user_data['id'], "购买课程包消耗积分",0);
                }

            }

            $this->success("添加成功");
        }else{
            $this->error("添加失败");
        }
    }
    public function wrapRechargeList(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $list = Db::name("course_wraprecharge")->where(['merchant_user_id'=>$merchant_id])->order("id desc")->paginate()->toArray();
        // $list = $list->toArray();
        // var_dump($list);exit;
        //  var_dump($list['data']);
        foreach ($list['data'] as $k=>$v){
            // var_dump($v);exit;
            $list['data'][$k]['image'] =  cdnurl($v['image'], true);
        }
        // 将数据转化为数组
        //$list = $list->toArray();
        // 返回数据
        $this->success('课程包列表', $list);
    }

    /**
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 课程包订单
     */
    public function wrapRechargeCard(){
        $user = $this->auth->getUser();
        $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->whereOr(['examine_user_ids_str'=>['like','%,'.$user->id.',%']])->column('id');
//echo Db::name("merchant")->getLastSql();
       // $list = Db::name("course_order")->where(['merchant_user_id'=>['in',$merchant_id]])->order("id desc")->paginate()->toArray();
       // $merchant_id = Db::name("merchant")->where(['user_id'=>$user->id])->value('id');
        $where['card.merchant_user_id'] = ['in',$merchant_id];
        $where['card.type'] = 3;
        $course_status = $this->request->post("course_status",0);
        if($course_status == 0){
            $where['card.status'] = ['in','1,2'];
        }else{
            $where['card.status'] = 3;
        }
        $name = $this->request->post("name");
        $nickname = $this->request->post("nickname");
        if($name){
            $where['c.name'] = $name;
        }
        if($nickname){
            $where['u.nickname'] = $nickname;
        }
        $list = Db::name("user_recharge_card")->alias("card")->where($where)->join("course_wraprecharge c",'card.order_id = c.id', 'LEFT')
            ->join("user u",'card.user_id = u.id', 'LEFT')
            ->field("card.id,card.sum,card.surplus_sum,c.name,c.image,u.nickname,card.status,c.course_wrap_id")
            ->order("card.id desc")->paginate()->toArray();
        // $list = $list->toArray();
        // var_dump($list);exit;
        //  var_dump($list['data']);
        foreach ($list['data'] as $k=>$v){
            $course_wrap_data = Db::name("course_wrap")->where(['id'=>$v['course_wrap_id']])->find();
           // $user_data = Db::name("user")->where(['id'=>$v['user_id']])->find();
            $list['data'][$k]['image'] =  cdnurl($course_wrap_data['image'], true);
            //$list['data'][$k]['nickname'] = $user_data['nickname'];
           // $list['data'][$k]['name'] = $wraprecharge['name'];
        }
        // 将数据转化为数组
        //$list = $list->toArray();
        // 返回数据
        $this->success('课程包订单', $list);
    }
    public function getUserList(){
        $mobile = $this->request->post("mobile");
        $where['status'] = "normal";
        if($mobile){
            $where['mobile'] = ['like',"%".$mobile."%"];
        }
        $list = Db::name("user")->where($where)->select();
        $this->success("用户列表",$list);
    }
}
